package com.eascs.dubbo.api.internal;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.api.internal.UpInfoService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanBksyInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanGsInfo;
import com.eascs.admin.o2o.loan.entity.O2oLoanScInfo;
import com.eascs.admin.o2o.loan.service.O2oLoanBksyService;
import com.eascs.admin.o2o.loan.service.O2oLoanGsService;
import com.eascs.admin.o2o.loan.service.O2oLoanScService;
import com.eascs.common.util.StringUtil;

@Service
public class UpInfoServiceImpl implements UpInfoService{
	
	@Autowired
	O2oLoanGsService gsService;
	
	@Autowired
	O2oLoanBksyService o2oLoanBksyService;
	
	@Autowired
	O2oLoanScService scService;

	@Override
	public String updateUpSG(O2oLoanGsInfo t) {
		O2oLoanGsInfo gsInfo = gsService.getLoanapplyNo(t.getLoanapplyNo());
		if (StringUtil.isBlank(t.getIsOwnFarm())) {
			return "是否有自有农场不能为空";
		}
		if (StringUtil.isBlank(t.getIsFarmer())) {
			return "是否向固定农户采购不能为空";
		}
		if (StringUtil.isBlank(t.getIsOtherPurchase())) {
			return "是否向其他供菜商采购不能为空";
		}
		if (null != gsInfo) {
			gsInfo.setIsOwnFarm(t.getIsOwnFarm());
			gsInfo.setOfficeprovince(t.getOfficeprovince());
			gsInfo.setOfficecity(t.getOfficecity());
			gsInfo.setOfficecounty(t.getOfficecounty());
			gsInfo.setDetailAddress(t.getDetailAddress());
			gsInfo.setIsFarmer(t.getIsFarmer());
			gsInfo.setIsOtherPurchase(t.getIsOtherPurchase());
			gsInfo.setOtherMode(t.getOtherMode());
			gsInfo.setOtherModeRemark(t.getOtherModeRemark());
			gsService.updateEntity(gsInfo);
		} else
			gsService.insertEntity(t);
		return null;
	}

	@Override
	public String updateUpBPJ(O2oLoanBksyInfo loanBksyInfo) {
		if (StringUtil.isBlank(loanBksyInfo.getSupplier())) {
			return "供应商不能为空";
		}
		if (null == loanBksyInfo.getPurchase()) {
			return "采购占比不能为空";
		}
		if (StringUtil.isBlank(loanBksyInfo.getCooperation())) {
			return "合作产品不能为空";
		}
		if (StringUtil.isBlank(loanBksyInfo.getPeriod())) {
			return "账期不能为空";
		}
		if (StringUtil.isBlank(loanBksyInfo.getId())) {
			loanBksyInfo.setIsDelete(StatusContexts.O2O_NO);
			o2oLoanBksyService.insertEntity(loanBksyInfo);
		} else {
			O2oLoanBksyInfo bksyInfo = o2oLoanBksyService.getEntityById(loanBksyInfo.getId());
			bksyInfo.setCooperation(loanBksyInfo.getCooperation());
			bksyInfo.setPeriod(loanBksyInfo.getPeriod());
			bksyInfo.setPurchase(loanBksyInfo.getPurchase());
			bksyInfo.setSupplier(loanBksyInfo.getSupplier());
			o2oLoanBksyService.updateEntity(bksyInfo);
		}
		return null;
	}

	@Override
	public String updateUPSC(O2oLoanScInfo t) {
		O2oLoanScInfo gsInfo = scService.getLoanapplyNo(t.getLoanapplyNo());
		if (StringUtil.isBlank(t.getTwoBatch())) {
			return "二批商不能为空";
		}
		if (StringUtil.isBlank(t.getSupermarket())) {
			return "商超不能为空";
		}
		if (StringUtil.isBlank(t.getDirectRetail())) {
			return "直接零售不能为空";
		}
		if (StringUtil.isBlank(t.getRetail())) {
			return "零售终端不能为空";
		}
		if (null != gsInfo) {
			gsInfo.setTwoBatch(t.getTwoBatch());
			gsInfo.setSupermarket(t.getSupermarket());
			gsInfo.setDirectRetail(t.getDirectRetail());
			gsInfo.setRetail(t.getRetail());
			scService.updateEntity(gsInfo);
		} else
			scService.insertEntity(t);
		return null;
	}

	@Override
	public void deleteUpBPJ(String id) {
		O2oLoanBksyInfo loanBksyInfo = o2oLoanBksyService.getEntityById(id);
		loanBksyInfo.setIsDelete(StatusContexts.O2O_YES);
		o2oLoanBksyService.updateEntity(loanBksyInfo);
	}

}
